package com.autodesk.akn.emr.web.controller;

import com.amazonaws.services.ec2.model.InstanceType;
import com.autodesk.akn.emr.dao.EMRDaoImpl;
import com.autodesk.akn.emr.web.entity.ClusterConfigurationDTO;
import com.autodesk.akn.emr.web.entity.EMRDescription;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

public class EMRController extends MultiActionController {

    private EMRDaoImpl emrDAO;
    private static List<String> instanceTypes = null;

    static {
        instanceTypes = new ArrayList();
        for (InstanceType instanceType : InstanceType.values()) {
            instanceTypes.add(instanceType.toString());
        }
    }

    public EMRDaoImpl getEmrDAO() {
        return emrDAO;
    }

    public void setEmrDAO(EMRDaoImpl emrDAO) {
        this.emrDAO = emrDAO;
    }

    @RequestMapping(value = "emr/list", method = RequestMethod.GET)
    public ModelAndView list(HttpServletRequest hsr, HttpServletResponse hsr1)
            throws Exception {
        ModelAndView model = new ModelAndView("emr/list");
        model.addObject("emrList", emrDAO.list());
        return model;
    }

    @RequestMapping(value = "emr/precreate", method = RequestMethod.GET)
    public ModelAndView precreate(HttpServletRequest hsr, HttpServletResponse hsr1)
            throws Exception {
        ModelAndView model = new ModelAndView("emr/create");
        model.addObject("instanceTypes", instanceTypes);
        return model;
    }

    @RequestMapping(value = "emr/create", method = RequestMethod.POST)
    public ModelAndView create(HttpServletRequest hsr, HttpServletResponse hsr1)
            throws Exception {
        ModelAndView model = new ModelAndView("emr/list");
        ClusterConfigurationDTO clusterConfiguration = new ClusterConfigurationDTO();
        clusterConfiguration.setJobName(hsr.getParameter("jobName"));
        clusterConfiguration.setInstances(Integer.parseInt(hsr.getParameter("numberOfInstances")));
        clusterConfiguration.setMasterType(hsr.getParameter("masterType"));
        clusterConfiguration.setSlaveType(hsr.getParameter("slaveType"));
        clusterConfiguration.setBootstrapAction(hsr.getParameter("bootstrapAction"));
        clusterConfiguration.setArguments(hsr.getParameter("arguments"));
        clusterConfiguration.setLogPath(hsr.getParameter("logPath"));
        clusterConfiguration.setPigEnabled(hsr.getParameter("pig") != null);
        clusterConfiguration.setHiveEnabled(hsr.getParameter("hive") != null);
        emrDAO.create(clusterConfiguration);
        model.addObject("emrList", emrDAO.list());
        return model;
    }

    @RequestMapping(value = "emr/terminate", method = RequestMethod.POST)
    public ModelAndView terminate(HttpServletRequest hsr, HttpServletResponse hsr1) throws Exception {
        ModelAndView model = new ModelAndView("emr/list");
        List<String> jobFlowIds = new ArrayList<String>();
        jobFlowIds.add(hsr.getParameter("jobFlowId"));
        emrDAO.terminate(jobFlowIds);
        model.addObject("emrList", emrDAO.list());
        return model;
    }

    @RequestMapping(value = "emr/getDescription", method = RequestMethod.GET)
    public @ResponseBody
    List<EMRDescription> getDescription(HttpServletRequest hsr, HttpServletResponse hsr1) throws Exception {
        return emrDAO.getDescription(hsr.getParameter("jobFlowId"));
    }
}
